package com.gang.mars.basic.parseannotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @Author：gang.chen
 * @Date：2023/5/6 12:54
 */
final class GAnnotationTypeMapping {

    private final int[] aliasMappings;

    private final int[] conventionMappings;

    private final Map<Method, List<Method>> aliasedBy;

    private final GAttributeMethod attributeMethod;

    private final Class<? extends Annotation> annotation;

    GAnnotationTypeMapping(Class<? extends Annotation> annotation)
    {
        this.annotation = annotation;
        /**
         * 1，每一个注解，最终都会封装为：AnnotationTypeMapping
         * 例如：SpringBootApplication -> AnnotationTypeMapping;
         * 2，attributeMethod 会装载注解本身及注解中所有方法
         *
         * 3，从注解中获取方法，按照方法名称排序
         * */
        attributeMethod = GAttributeMethod.compute(annotation);
        /**
         * 1，aliasMappings是一个int类型数组，其数组长度等于方法的长度
         * 例如：SpringBootApplication 注解中有6个方法，则aliasMappings长度为6， 每个数组的值赋值为-1
         * */
        this.aliasMappings = filledIntArray(attributeMethod.size());
        this.conventionMappings = filledIntArray(attributeMethod.size());

        this.aliasedBy = resolveAliasedForTarget();
        
        processAliases();
    }

    private Map<Method, List<Method>> resolveAliasedForTarget() {

        Map<Method, List<Method>> aliasedBy = new HashMap<>();

        for (int i = 0; i < this.attributeMethod.size(); i++) {

            Method method = attributeMethod.get(i);


        }

        return Collections.unmodifiableMap(aliasedBy);
    }

    private void processAliases() {

        List<Member> aliases = new ArrayList<>();

        for (int i = 0; i < this.attributeMethod.size(); i++) {
            
            aliases.clear();
            
            aliases.add(this.attributeMethod.get(i));
            
            collectAliases(aliases);
        }
        
    }

    private void collectAliases(List<Member> aliases) {
        GAnnotationTypeMapping mapping = this;

        /**
         * _____________________________________________________________________
         * 场景1：
         * 如果注解=GSpringBootApplication，完成第一循环结束，因为该根结点source = null
         * _____________________________________________________________________
         * 场景2：
         * 如果注解=GComponentScan，要一解析到根注解，当source = null是结束该循环
         * */
        while (mapping != null){



        }
    }

    public Class<? extends Annotation> getAnnotation() {
        return annotation;
    }

    private static int[] filledIntArray(int size)
    {
        int[] array = new int[size];
        Arrays.fill(array, -1);
        return array;
    }
}
